package cj.web.admin.controllers;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.web.admin.dao.AuditStepConfigDAO;
import cj.web.admin.dao.UserInfoDAO;
import cj.web.admin.domain.AuditStepConfig;
import cj.web.admin.domain.UserInfo;
import cj.web.constants.MemuEnum;
import cj.web.login.controllers.BaseController;

@Path("/auditStepConfig")
public class AuditStepConfigController extends BaseController{

	private static Logger logger = Logger.getLogger(AuditStepConfigController.class.getName());
	
	@Autowired
	private AuditStepConfigDAO auditStepConfigDao;
	@Autowired
	private UserInfoDAO userInfoDao;
	
	@Get("show")
	public String show(Model model,AuditStepConfig auditStepConfig) {
	
		//MemuEnum
        model.add("MemuEnum",MemuEnum.shrybz);
		return "/views/config/auditStepConfigList.vm";
	}

	@Post("getList")
	public String queryList(AuditStepConfig auditStepConfig) {
	
		List<Map> arr = auditStepConfigDao.query(auditStepConfig);
		
		for(Map obj:arr)
		{
			
			obj.put("createAt", DateUtils.getMinusTime(obj.get("createAt").toString(), DateUtils.DATE_FULL_STR, 0));
			obj.put("updateAt", DateUtils.getMinusTime(obj.get("updateAt").toString(), DateUtils.DATE_FULL_STR, 0));
		}

		JSONObject jo = new JSONObject();

		int size = auditStepConfigDao.querySize(auditStepConfig);

		jo.put("data", arr);
		jo.put("draw", auditStepConfig.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();	
	}
	
	@Get("createPage")
	public String createPage(Model model)
	{	//MemuEnum
        model.add("MemuEnum",MemuEnum.shrybz);
		return "/views/config/auditStepConfigDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,AuditStepConfig auditStepConfig,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			auditStepConfig.setCreateUser(returnSessionObj(request).getAccount());
			auditStepConfig.setUpdateUser(returnSessionObj(request).getAccount());
			auditStepConfigDao.create(auditStepConfig);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id)
	{
		AuditStepConfig obj = auditStepConfigDao.queryById(id);
		
		if(obj!=null){
			model.add("obj",obj);
		}
		//MemuEnum
        model.add("MemuEnum",MemuEnum.shrybz);
		return "/views/config/auditStepConfigEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,AuditStepConfig auditStepConfig,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			auditStepConfig.setUpdateUser(returnSessionObj(request).getAccount());
			auditStepConfigDao.update(auditStepConfig);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id)
	{
		AuditStepConfig obj = auditStepConfigDao.queryById(id);
		
		if(obj!=null)
		{
			auditStepConfigDao.deleteById(id);
		}
		return "r:show";
	}
	
	/**
	 * 选择 审核人员
	 * @param model
	 * @return
	 */
	@Get("showChooseUse")
	public String showChooseUse(Model model,@Param("id") int id)
	{
		AuditStepConfig obj = auditStepConfigDao.queryById(id);
		
		if(obj!=null){
			model.add("obj",obj);
		}
		//MemuEnum
        model.add("MemuEnum",MemuEnum.shrybz);
		return "/views/config/auditStepConfigPer.vm";
	}

	/**
	 * 审核人员List
	 * @param userInfo
	 * @return
	 */
	@Post("getChooseUseList")
	public String getChooseUseList(UserInfo userInfo) {
		
		JSONObject jo = new JSONObject();
		
		List<Map> objs = userInfoDao.query(userInfo);
		
		int size = userInfoDao.querysize(userInfo);
		jo.put("data", objs);
		jo.put("draw", userInfo.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();
	}
	
	@Get("saveChooseUse")
	public String saveChooseUse(@Param("stepId") int stepId,@Param("userId") int userId) {
		
		AuditStepConfig obj = auditStepConfigDao.queryById(stepId);
		
		if(obj!=null)
		{
			auditStepConfigDao.updateASPerById(stepId,userId);
		}
		
		return "r:show";
	}
}
